<?php

namespace app\api\model;

use think\Model;

class Friends extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 朋友圈列表
     * @param $user_id
     * @return \think\Paginator
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function friendsList($user_id)
    {
        $data = model('attention')
            ->alias('a')
            ->join('dynamic b', 'a.attention_id=b.user_id')
            ->join('user_info c', 'a.attention_id=c.user_id')
            ->where('a.user_id', $user_id)
            ->order('b.createtime desc')
            ->field("b.id,b.user_id,b.address,b.content,b.images,c.avatar,FROM_UNIXTIME(b.createtime,'%Y-%m-%d %H:%i:%s') as createtime,
                        c.birthday,c.username,c.gender,c.status as auth_status")
            ->paginate(10);
        foreach ($data as $k => $v) {
            $data[$k]['distance'] = '12km';//距离
            $data[$k]['user_age'] = user_age($v['birthday']);//年龄
            $data[$k]['members'] = model('members')->where('user_id', $user_id)->value('status') ?? 0;//会员等级
            $data[$k]['comment_num'] = model('dynamic_comment')->where('dynamic_id', $v['id'])->count();//评论数量
            $data[$k]['like_num'] = model('dynamic_like')->where('dynamic_id', $v['id'])->count();//点赞数量
            $data[$k]['friends'] = $this->where(['user_id' => $user_id, 'friends_id' => $v['user_id']])->find() ? 1 : 0;
            $data[$k]['like_status'] = model('dynamic_like')->where(['user_id' => $user_id, 'dynamic_id' => $v['id']])->find() ? 1 : 0;//判断是否已点赞
            //评论列表
            $comment_list = commentList($v['id']);
            $data[$k]['comment_list'] = $comment_list;
        }
        return $data;
    }
}